<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script>
        //原型式继承
        function Student(name){
            this.name=name;
        }
        var stu=new Student("alice");
        //方式一：动态向原型对象中添加属性和方法
        Student.prototype.age=30;
        Student.prototype.sex="男";
        Student.prototype.run=function(){
            console.log("我在跑步");
        };
        console.log(stu.age);
        console.log(stu.sex);
        stu.run();

        //方式二：直接覆盖原型对象,只针对覆盖之后创建的对象有效
        // 新增的属性和方法对覆盖之后创建的对象有效
        Student.prototype={
            constructor:Student,//如果希望覆盖之后创建的对象是类型相关的，必须添加constructor
            hobby:"吃饭",
            study:function(){
                console.log("我爱学习");
            }
        };
        var stu2=new Student("cici");
        console.log(stu.age);//30
        console.log(stu2.age);//undefined
        console.log(stu.sex);//男
        console.log(stu2.sex);//undefined
        console.log(stu.hobby);//undefined
        console.log(stu2.hobby);//吃饭
        console.log(stu2.constructor.name);
    </script>
</head>
<body>
    
</body>
</html>